package com.zf.bm.module.convert;

import org.mapstruct.InheritConfiguration;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * MapStruct 公共层
 * @author sk
 * @since 2025/05/29
 * @param <D>
 * @param <E>
 */
public interface BaseMapper<D,E>{
    /**
     * entity转 DTO
     *
     * @param entity
     * @return
     */
    D toDto(E entity);

    /**
     * DTO 转 entity
     *
     * @param dto
     * @return
     */
    E toEntity(D dto);

    /**
     * entity集合 转 DTO集合
     *
     * @param entityList
     * @return
     */
   default List<D> toDto(List<E> entityList){
       if (entityList == null) {
           return null;
       }

       List<D> result = new ArrayList<D>(entityList.size());
       for (E s : entityList) {
           if (s == null) {
               continue;
           }
           result.add(toDto(s));
       }

       return result;
    }

    /**
     * DTO集合 转 entity集合
     *
     * @param dtoList
     * @return
     */
    default List<E> toEntity(List<D> dtoList){
        if (dtoList == null) {
            return null;
        }

        List<E> result = new ArrayList<E>(dtoList.size());
        for (D t : dtoList) {
            if (t == null) {
                continue;
            }
            result.add(toEntity(t));
        }

        return result;
    }

    /**
     * 正向映射（stream)
     * @param stream
     * @return
     */
    @InheritConfiguration(name = "convertTo")
    Stream<D> convertTo(Stream<E> stream);

    /**
     * 反向映射（stream)
     * @param stream
     * @return
     */
    @InheritConfiguration(name = "convertFrom")
    Stream<E> convertFrom(Stream<D> stream);
}
